<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="分类" prop="catalogId">
        <category-cascader ref="catSelect" @change="catSelectChange"/>
      </el-form-item>
      <el-form-item prop="brandId">
        <span slot="label">
                <el-tooltip content="需要先指定所属分类" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                品牌
              </span>
        <brand-select ref="brandSelect" :value="queryParams.brandId" @change="brandChange"/>
      </el-form-item>
      <el-form-item label="关键字" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格区间">
        <el-input-number v-model="queryParams.minPrice" placeholder="最低" :min="0" :precision="2"
                         controls-position="right"
        />
      </el-form-item>
      <el-form-item>
        <el-input-number v-model="queryParams.maxPrice" placeholder="最高" :min="0" :precision="2"
                         controls-position="right"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" row-key="skuId">
      <el-table-column width="50" type="expand">
        <template slot-scope="props">
          <div style="margin: 10px">
            <el-descriptions :column="1" title="详细信息">
              <el-descriptions-item label="标题">{{ props.row.skuTitle }}</el-descriptions-item>
              <el-descriptions-item label="副标题"><span v-html="props.row.skuSubtitle"></span></el-descriptions-item>
              <el-descriptions-item label="spuId">{{ props.row.spuId }}</el-descriptions-item>
              <el-descriptions-item label="分类编号">{{ props.row.catalogId }}</el-descriptions-item>
              <el-descriptions-item label="品牌编号">{{ props.row.brandId }}</el-descriptions-item>
              <el-descriptions-item label="描述">{{ props.row.skuDesc }}</el-descriptions-item>
            </el-descriptions>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="编号" align="center" prop="skuId"/>
      <el-table-column label="名称" align="center" prop="skuName"/>
      <el-table-column
        label="默认图片"
        align="center"
      >
        <template #default="scope">
          <image-preview :src="scope.row.skuDefaultImg" :split="false" :width="'45px'" :height="'45px'"/>
        </template>
      </el-table-column>
      <el-table-column label="spu编号" align="center" prop="spuId"/>
      <el-table-column label="分类" align="center" prop="catalogName"/>
      <el-table-column label="品牌" align="center" prop="brandName"/>
      <el-table-column label="价格￥" align="center" prop="price"/>
      <el-table-column label="销量" align="center" prop="saleCount"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
          >预览
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-chat-dot-square"
          >评论
          </el-button>
          <el-dropdown @command="handleCommand(scope.row,$event)">
            <el-button type="text" size="mini">
              更多<i class="el-icon-arrow-down"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="uploadImages">上传图片</el-dropdown-item>
              <el-dropdown-item command="seckillSettings">参与秒杀</el-dropdown-item>
              <el-dropdown-item command="reductionSettings">满减设置</el-dropdown-item>
              <el-dropdown-item command="discountSettings">折扣设置</el-dropdown-item>
              <el-dropdown-item command="memberPriceSettings">会员价格</el-dropdown-item>
              <el-dropdown-item command="stockSettings">库存管理</el-dropdown-item>
              <el-dropdown-item command="couponSettings">优惠劵</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import BrandSelect from '@/components/my/BrandSelect'

export default {
  name: 'index',
  components: { BrandSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        catalogId: undefined,
        brandId: undefined,
        key: undefined,
        minPrice: undefined,
        maxPrice: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      uri: '/product/skuInfo'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      this.$api.get(this.uri, this.queryParams).then(({ data }) => {
        this.list = data.records
        this.total = data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.minPrice = undefined
      this.queryParams.maxPrice = undefined
      this.$refs.brandSelect.clear()
      this.$refs.catSelect.reset()
      this.handleQuery()
    },
    catSelectChange(arr) {
      this.queryParams.catalogId = undefined
      this.queryParams.brandId = undefined
      this.$refs.brandSelect.clear()
      if (arr.length) {
        // 获取选中的分类
        const catId = arr[arr.length - 1]
        this.queryParams.catalogId = catId
        // 查询对应的品牌
        this.$refs.brandSelect.getBrands(catId)
      }
    },
    brandChange(val) {
      this.queryParams.brandId = val
    },
    handleCommand(row, command) {
      console.log(row, command)
    }
  }
}
</script>

<style scoped>
/* 表格扩展列 */
/deep/ .el-table__expand-column {
}
</style>
